package com.example.madman.yuejian.Fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.example.madman.yuejian.Adapter.ActivityListAdapter;
import com.example.madman.yuejian.Bean.CourseBean;
import com.example.madman.yuejian.Listener.RetrofitListener;
import com.example.madman.yuejian.Model.Tmodel;
import com.example.madman.yuejian.R;

import java.util.ArrayList;
import java.util.List;


public class ActivityListFragment extends Fragment {
    private RecyclerView recyclerView;
    private ActivityListAdapter activityListAdapter;
    private List<CourseBean> list;
    private RecyclerView.LayoutManager layoutManager;
    private Context context;
    private RetrofitListener<List<CourseBean>> listener;
    private Tmodel model;

    public ActivityListFragment() {

    }




    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_item_activitylist, container, false);
        context=getActivity();
        recyclerView=(RecyclerView)view.findViewById(R.id.list_course);
        init1();
        init2();
        init3();
        conhttp();
        return view;
    }



    private void init1(){
        listener = new RetrofitListener<List<CourseBean>>() {
            @Override
            public void onSuccess(List<CourseBean> courseBeen) {
                list=courseBeen;
                activityListAdapter.setList(list);
            }

            @Override
            public void onFail(String msg) {
                Toast.makeText(context,msg,Toast.LENGTH_SHORT).show();
            }
        };
    }

    private void init2(){
        list=new ArrayList<CourseBean>();
        layoutManager=new LinearLayoutManager(context);
        recyclerView.setLayoutManager(layoutManager);


    }

    private void init3(){

        activityListAdapter = new ActivityListAdapter(context);
        activityListAdapter.setList(list);
        recyclerView.setAdapter(activityListAdapter);
    }
    private void conhttp(){
        model = new Tmodel("gson");
        model.getCourseList(listener);
    }
}
